<?
/**
* Create the minimalistic connection
* And data tranfer to Oracle
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @copyright Copyright (c)2007, Adesanto Asman
* @package Oracle
*
*/

include_once "DbVars.php";

define("_DEBUG_MODE"        , 0); /* 1 = _DEBUG_MODE, 0 = _IS_SILENT */

/**
* Extends DbVars
* @package Oracle
* @see DbVars
*/
class Oracle extends DbVars{

  /**
  * Init all necessary variables
  * @param string $userName
  * @param string $userPwd
  * @param string $userSid
  * @param string $userComment
  *
  */
  function init($userName,
                $userPwd,
                $userSid,
                $userComment){

    $this->userName     = $userName;
    $this->userPwd      = $userPwd;
    $this->userSid      = $userSid;
    $this->userComment  = $userComment;
  }

  /**
  * It returns false if fail
  * @param void
  * @return boolean
  *
  */
  function connect(){

    if($dbConn = OCILogon($this->userName, $this->userPwd, $this->userSid)){

      $this->dbConn = $dbConn;
      return true;
    }else{

      return false;
    }
  }

  /**
  * Disconnect the DB
  * @param void
  *
  */
  function dc(){

    OCILogoff($this->dbConn);
  }

  /**
  * Execute the query and the results
  * will be passed by reference to &$nrows and &$result
  *
  * @param string   $query
  * @param integer  $nrows
  * @param array    $result
  * @param boolean  $isDbg
  *
  */
  function execFetchQuery( &$query, &$nrows, &$result, $isDbg = _DEBUG_MODE){

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    $s = OCIParse($this->dbConn, $query);
    $r = OCIExecute($s, OCI_DEFAULT);

    if($r == true){

      $nrows = oci_fetch_all($s, $result);

      $this->nrows  = $nrows;
      $this->result = $result;


      $this->status = true;
      return true;
    }else{

      $dbgInfo = debug_backtrace();
      if($isDbg){

        echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
      }

      $this->status = false;
      return false;
    }

  }

  /**
  * Exec query not a fetch type
  * it accomodates UPDATE, INSERT, DELETE, CREATE, MODIFY, etc
  * Which not returns any result of rows of data.
  *
  * @param string   $query
  * @param boolean  $isDbg true show the query,
                           false dont show.
  * @return boolean false when it fails
  *
  */
  function execQuery( $query,
                      $isDbg = _DEBUG_MODE){

    if($isDbg){

      $dbgInfo = debug_backtrace();
      echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
    }

    $s = OCIParse($this->dbConn, $query);
    $r = OCIExecute($s, OCI_DEFAULT);
    if($r == true){

      $this->status = true;
      return true;
    }else{

      $dbgInfo = debug_backtrace();
      if($isDbg){

        echo	"<br/>(".$dbgInfo[0]["file"].":".$dbgInfo[0]["line"].") ".$query." <br/>\n";
      }

      $this->status = false;
      return false;
    }
  }

  /**
  *
  * @param void
  * @return string
  *
  */
  function getStrConn(){

    return $this->dbConn;
  }

  /**
  *
  * @param void
  *
  */
  function commit(){

    if($this->status == true){

      OCICommit($this->dbConn);
    }
  }

  /**
  * get the status
  *
  * @param void
  * @return boolean
  *
  */
  function getStatus(){

    return $this->status;
  }

  /**
   * set Status manually
   *
   * @param boolean $status
   */
  function setStatus($status){

    $this->status = $status;
  }
}
?>